import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mvvm_provider/page/home/data/model/cartoon_model.dart';
import '../../../widgets/app_net_image.dart';

class ShowNovelsItemWidget extends StatelessWidget {
  final int index;
  final CartoonModel model;
  final Function collectionTap;

  const ShowNovelsItemWidget(
      {super.key, required this.index, required this.model, required this.collectionTap});

  /// 图片
  Widget _imageWidget() {
    return AppNetImage(
      radius: 6.h,
      fit: BoxFit.cover,
      width: (1.sw - 30.w) / 2,
      imageUrl: model.image ?? '',
    );
  }

  /// 标题
  Widget _titleWidget() {
    return Text(
      model.title ?? '',
      overflow: TextOverflow.ellipsis,
      style: TextStyle(color: Colors.black, fontWeight: FontWeight.w500, fontSize: 16.sp),
    );
  }

  /// 描述
  Widget _desWidget() {
    String textStr = '';
    if ((model.comicInfo!.tags ?? []).isNotEmpty) {
      if (model.comicInfo!.main_style_name!.isNotEmpty) {
        textStr = "${model.comicInfo?.main_style_name} | ${model.comicInfo?.tags![0]}";
      } else {
        textStr = "${model.comicInfo?.tags![0]}";
      }
    } else {
      if (model.comicInfo!.main_style_name!.isEmpty) {
        textStr = "暂无描述";
      } else {
        textStr = model.comicInfo?.main_style_name ?? "";
      }
    }
    return Text(
      textStr,
      overflow: TextOverflow.ellipsis,
      style: TextStyle(color: Colors.black45, fontWeight: FontWeight.w500, fontSize: 14.sp),
    );
  }

  Widget _collectionWidget() {
    return InkWell(
      onTap: () => collectionTap(),
      child: Image.asset(
        "assets/images/no_collention.png",
        fit: BoxFit.fitWidth,
        width: 18.w,
        height: 18.w,
        color: (model.isCollection ?? false) ? Colors.yellow : Colors.blueGrey,
      ),
    );
  }

  Widget _otherWidget() {
    return Container(
      height: 60.h,
      width: (1.sw - 30.w) / 2,
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.symmetric(horizontal: 6.w, vertical: 6.w),
      decoration: BoxDecoration(
          color: Colors.white,
          border: Border.all(width: 1.w, color: Colors.black12),
          borderRadius: BorderRadius.only(
              bottomLeft: Radius.circular(6.h), bottomRight: Radius.circular(6.h))),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _titleWidget(),
          SizedBox(height: 4.h),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [_desWidget(), _collectionWidget()],
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        padding: const EdgeInsets.all(0),
        decoration: BoxDecoration(
          color: const Color(0xFFF3F4F8),
          borderRadius: BorderRadius.all(Radius.circular(6.h)),
        ),
        alignment: Alignment.center,
        child: Stack(
          children: [_imageWidget(), Positioned(bottom: 0, child: _otherWidget())],
        ));
  }
}
